package com.maoye.ossp.common.web.taglib;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * *
 * 
 * @author xiaohan
 * @date 2014年3月31日
 * @version
 * @filename com.maoye.ossp.common.web.taglib.UpLoadTag.java
 */
public class DownFileTag extends TagSupport {

	Logger logger = Logger.getLogger(DownFileTag.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 8033078121407287559L;
	private String prefixURL = "/sys/att";
	private int indexInThisPage;
	/**
	 * 处理的下载的URL
	 */
	private String handlerDownloadURL = prefixURL + "/download";
	/**
	 * 下载列表
	 */
	private String downloadFileListURL = prefixURL + "/attr/download/list";

	private String modleName;
	private String key;
	private String soureBillID;
	private String isShowDel;
	private String isShowCreateTime;
	
	
	
	/**
	 * @return the isShowCreateTime
	 */
	public String getIsShowCreateTime() {
		return isShowCreateTime;
	}

	/**
	 * @param isShowCreateTime the isShowCreateTime to set
	 */
	public void setIsShowCreateTime(String isShowCreateTime) {
		this.isShowCreateTime = isShowCreateTime;
	}


	
	
	/**
	 * @return the isShowDel
	 */
	public String getIsShowDel() {
		return isShowDel;
	}

	/**
	 * @param isShowDel the isShowDel to set
	 */
	public void setIsShowDel(String isShowDel) {
		this.isShowDel = isShowDel;
	}

	/**
	 * 
	 * @author xiaohan
	 * @data 2014年3月31日
	 */
	private void iniPath() {
		handlerDownloadURL = prefixURL + "/download";
		downloadFileListURL = prefixURL + "/attr/download/list";
	}

	public int doStartTag() {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext
					.getRequest();

			if (!prefixURL.startsWith(request.getContextPath())) {
				prefixURL = request.getContextPath() + prefixURL;
				iniPath();
			}

			StringBuilder sb = new StringBuilder();
			if (indexInThisPage == 0) {
				sb.append("<script type=\"text/javascript\">\r\n");
				sb.append("function deleteAtt(t,url){");
				sb.append("	var tr = $(t).closest(\"tr\");");
				sb.append("var path =Com_Parameter.ContextPath + url;");
				sb.append("$.get(Com_Parameter.ContextPath + url,function(data) {");
				sb.append(" });");
				sb.append("tr.remove();}");
				sb.append("\tfunction Att_LoadFile(modleName,key,soureBillID){\r\n");
				sb.append("\t\tvar downloadFileListURL = \"");
				sb.append(request.getContextPath());
				sb.append("/sys/att/attr/download/list?modleName=\"+modleName+\"&key=\"+key+\"&soureBillID=\"+soureBillID;\r\n");
				sb.append("\t\t$.ajax({\r\n");
				sb.append("\t\t\turl: downloadFileListURL,\r\n");
				sb.append("\t\t\tsuccess:function(data){\r\n");
				sb.append("\t\t\t\tdata = {o:eval(\"(\"+data+\")\")};\r\n");
			
				sb.append("\t\t\t\tdata.o.formatFileSize = function(size){\r\n");
				sb.append("        \t\t\treturn (size / 1000)+'KB';\r\n");
				sb.append("        \t\t};\r\n");
				sb.append("\t\t\t\tdata.o.formatFileIcon = function(fileType){\r\n");
				sb.append("\t\t\t\t\tswitch(fileType){\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/msword\":return \"doc_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\":return \"doc_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/vnd.ms-excel\":return \"xls_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\":return \"xls_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/vnd.ms-powerpoint\":return \"pdf_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/pdf\":return \"pdf_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"text/plain\":return \"text_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/x-jpg\":return \"img_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/x-ico\":return \"img_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"application/x-bmp\":return \"img_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"image/gif\":return \"img_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"image/png\":return \"img_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tcase \"image/jpeg\":return \"img_1.png\";\r\n");
				sb.append("\t\t\t\t\t\tdefault:return \"documents_1.png\";\r\n");
				sb.append("\t\t\t\t\t}\r\n");
				sb.append("        \t\t};\r\n");
				sb.append("\t\t\t\tvar html = template('file-view-tmp', data);\r\n");
				sb.append("\t\t\t\t$(\"#file-view-\"+key).html(html);\r\n");
				sb.append("\t\t\t},\r\n");
				sb.append("\t\t\tfailure:function(){\r\n");
				sb.append("\t\t\t\talert('数据请求失败!请重试');\r\n");
				sb.append("\t\t\t\twindow.close();\r\n");
				sb.append("\t\t\t}\r\n");
				sb.append("\t\t});\r\n");
				sb.append("\t}\r\n");
				sb.append("</script>\r\n");
				sb.append("<script id=\"file-view-tmp\" type=\"text/html\">\r\n");
				sb.append("\t{{each o.files as file}}\r\n");
				sb.append("\t\t  <tr>\r\n");
				sb.append("               <td><span class=\"preview img_preview\"><img src=\"");
				sb.append(request.getContextPath() + "/static");
				sb.append("/images/icon/fileIcon/{{o.formatFileIcon(file.fileType)}}\" height=\"26\" width=\"26\"></span></td>\r\n");
				sb.append("               <td><p class=\"name\">{{file.name}}</p></td>\r\n");
				sb.append("               <td><p class=\"size\">{{o.formatFileSize(file.size)}}</p> </td>\r\n");
				sb.append("               <td> \r\n");
				sb.append("\t\t\t\t\t<a href=\"");
				sb.append(request.getContextPath());
				sb.append("{{file.url}}\" title=\"{{file.name}}\">\r\n");
				sb.append("                   <button type=\"submit\" class=\"btn btn-warning img_padding_btn img_margin btn_3\">\r\n");
				sb.append("                       <i class=\"glyphicon glyphicon-download-alt\"></i>\r\n");
				sb.append("                       <span>&nbsp;下&nbsp;载</span>\r\n");
				sb.append("                   </button>\r\n");
				sb.append("\t\t\t\t\t<a href=\"");
				if("true".equals(isShowDel)){
					sb.append(request.getContextPath());
					sb.append("{{file.delete_url}}\" title=\"{{file.name}}\">\r\n");
					sb.append("                   <a type=\"button\" onclick=\"deleteAtt(this,'{{file.delete_url}}')\"  class=\"btn btn-danger btn_3 img_margin\">\r\n");
					sb.append("                       <i class=\"glyphicon glyphicon-remove-sign\"></i>\r\n");
					sb.append("                       <span>&nbsp;删&nbsp;除</span>\r\n");
					sb.append("                   </a>\r\n");
					sb.append("\t\t\t\t\t</a>\r\n");
					sb.append("                </td>\r\n");
				}
				sb.append("           </tr>\r\n");
				sb.append("\t{{/each}}\r\n");
				sb.append("</script>\r\n");
				
				
			
			}
			sb.append("<div class=\"bs-example\" name=\"ossp-attachment-contr\">\r\n");
			sb.append("    <table role=\"presentation\" class=\"img_table padding\">\r\n");
			sb.append("       <tbody id=\"file-view-" + key + "\">\r\n");
			sb.append("       </tbody>\r\n");
			sb.append("   </table>\r\n");
			sb.append("   <script type=\"text/javascript\">\r\n");
			sb.append("\t\t$(document).ready(function (){\r\n");
			sb.append("\t\t\tAtt_LoadFile(\"" + modleName + "\",\"" + key
					+ "\",\"" + soureBillID + "\");\r\n");
			sb.append("\t\t});\r\n");
			sb.append("\t</script>\r\n");
			sb.append("</div>\r\n");
			pageContext.getOut().print(sb.toString());
		} catch (Exception e) {
			logger.debug(e);
		}
		return EVAL_BODY_INCLUDE;

	}

	public String getModleName() {
		return modleName;
	}

	public void setModleName(String modleName) {
		this.modleName = modleName;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getSoureBillID() {
		return soureBillID;
	}

	public void setSoureBillID(String soureBillID) {
		this.soureBillID = soureBillID;
	}

	public String getHandlerDownloadURL() {
		return handlerDownloadURL;
	}

	public void setHandlerDownloadURL(String handlerDownloadURL) {
		if (StringUtils.isBlank(handlerDownloadURL)) {
			return;
		}
		this.handlerDownloadURL = handlerDownloadURL;
	}

	public String getDownloadFileListURL() {
		return downloadFileListURL;
	}

	public void setDownloadFileListURL(String downloadFileListURL) {
		if (StringUtils.isBlank(downloadFileListURL)) {
			return;
		}
		this.downloadFileListURL = downloadFileListURL;
	}

	public int getIndexInThisPage() {
		return indexInThisPage;
	}

	public void setIndexInThisPage(int indexInThisPage) {
		this.indexInThisPage = indexInThisPage;
	}

}
